<?php

namespace app\admin\controller\api;


use app\common\controller\BaseApi;
use app\common\controller\BaseWechatApi;

class Api extends BaseWechatApi {

    public $memberId = '';
    public function _initialize()
    {
        parent::_initialize();
        $member = model('Member')->where('tel', session('wechat_user'))->find();
        $this->memberId = $member->id;
    }

    public function saveActivity() {
        $activityName = request()->post('activityName');
        $activityContent = request()->post('activityContent');
        $startTime = request()->post('startTime');
        $endTime = request()->post('endTime');
        $result = model('MettingLog')->save([
            'activity_name'=> $activityName,
            'activity_content'=>$activityContent,
            'start_time'=>$startTime,
            'end_time'=>$endTime,
            'status'=>0,
            'member_id'=>$this->memberId
        ]);
        if ($result < 1) {
            return json(['code'=>1, 'msg'=>'提交失败', 'data'=>'']);
        }
        return json(['code'=>0, 'msg'=>'提交成功', 'data'=>$result]);
    }

    public function getActivitys() {
        return json(model('ActivityApply')->where('member_id', $this->memberId)->select());
    }

    public function getMettingLogs() {
        return json(model('MettingLog')
            ->with('Member')
            ->with('MettingRoom')
            ->where('member_id', $this->memberId)
            ->select());
    }

    public function saveMetting () {
        $remark = request()->post('remark');
        $mettingTime = request()->post('mettingTime');
        $roomId = request()->post('roomId');
        $status = request()->post('status');
        $memberId = $this->memberId;
        $result = model('MettingLog')->save([
            'remark'=> $remark,
            'metting_time'=>$mettingTime,
            'room_id'=>$roomId,
            'status'=>0,
            'member_id'=>$this->memberId
        ]);
        if ($result < 1) {
            return json(['code'=>1, 'msg'=>'提交失败', 'data'=>'']);
        }
        return json(['code'=>0, 'msg'=>'提交成功', 'data'=>$result]);
    }

    public function getMettings () {
        return json(model('MettingRoom')->select());
    }

    public function saveReleaseApply () {
        $phone = request()->post('phone');
        $applyTime = request()->post('applyTime');
        $cardNo = request()->post('cardNo');
        $result = model('ReleaseApply')->save([
            'car_no'=> $cardNo,
            'phone'=>$phone,
            'apply_time'=>$applyTime,
            'member_id'=>$this->memberId
        ]);
        if ($result < 1) {
            return json(['code'=>1, 'msg'=>'提交失败', 'data'=>'']);
        }
        return json(['code'=>0, 'msg'=>'提交成功', 'data'=>$result]);
    }

    public function releaseApplys () {
        $releaseApplys = model('ReleaseApply')->where('member_id', $this->memberId)->select();
        return json($releaseApplys);
    }

    public function saveDecoration () {
        $communityCode = request()->post('communityCode');
        $houseId = request()->post('houseId');
        $result = model('DecorationApply')->save([
            'community_code'=>$communityCode,
            'house_id'=>$houseId,
            'member_id'=>$this->memberId
        ]);
        if ($result < 1) {
            return json(['code'=>1, 'msg'=>'提交失败', 'data'=>'']);
        }
        return json(['code'=>0, 'msg'=>'提交成功', 'data'=>$result]);
    }

    public function getDecorations () {
        $decorations = model('DecorationApply')
            ->with('Member')
            ->with('Building')
            ->with('Community')
            ->with('House')
            ->where('member_id', $this->memberId)
            ->select();
        return json($decorations);
    }

    public function saveSuggestion () {
        $content = request()->post('content');
        $result = model('Suggestions')->save([
            'content'=>$content,
            'member_id'=>$this->memberId
        ]);
        if ($result < 1) {
            return json(['code'=>1, 'msg'=>'提交失败', 'data'=>'']);
        }
        return json(['code'=>0, 'msg'=>'提交成功', 'data'=>$result]);
    }

    public function getSuggestions () {
        return json(model('Suggestions')->where('member_id', $this->memberId)->select());
    }

    public function getCommunityList () {
        return json(model('Community')->select());
    }

    public function getHouseByCId () {
        $CId = request()->get('CId');
        return json(model('House')->where('community_code', $CId)->select());
    }

    public function wechat_login () {
        // 这里暂时不做微信授权登录，只做手机关联验证，因为现在业务只涉及到业主和客户部分，都是在后台录入的
        $phone = request()->post('phone');
        $sms = request()->post('sms');
        if (empty($phone)){
            return json(['code'=>1, 'msg'=>'手机号不能为空', 'data'=>'']);
        }
        if ($sms != '1041') {
            return json(['code'=>1, 'msg'=>'请输入正确的验证码', 'data'=>'']);
        }
        $member = model('Member')->where('tel', $phone)->find();
        if (!isset($member)) {
            return json(['code'=>1, 'msg'=>'没有找到该用户', 'data'=>'']);
        }
        session('wechat_user', $phone);
        return json(['code'=>0, 'msg'=>'登录成功', 'data'=>$member]);
    }

    public function testLogin () {
//        if (!isset($_SESSION['wechat_user']) && empty($_SESSION['wechat_user'])) {
//            return json(['code'=>1, 'msg'=>'您未登录', 'data'=>'']);
//        }
//        return json(['code'=>0, 'msg'=>'主页内容', 'data'=>'']);
    }

    public function wechat_logout () {
        session_destroy();
    }
}